package org.lease.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema
public class Result<T> {
    // 请求是否成功
    @Schema(description = "请求是否成功")
    private boolean success;

    // 业务响应状态码
    @Schema(description = "业务响应状态码")
    private Integer code;

    // 业务响应信息
    @Schema(description = "业务响应信息")
    private String message;

    // 业务响应数据
    @Schema(description = "业务响应数据")
    private T data;

    // 私有化构造器
    private Result(boolean success, ResultCode resultCode, T data) {
        this.success = success;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    // 带数据的成功响应
    public static <T> Result<T> success(T data) {
        return new Result<>(true, ResultCode.SUCCESS, data);
    }

    // 不带数据的成功响应
    public static Result success() {
        return new Result<>(true, ResultCode.SUCCESS, null);
    }

    // 特定的失败响应
    public static Result error(ResultCode resultCode) {
        return new Result(false, resultCode, null);
    }

    // 默认的失败响应，需要传入自定的错误信息
    public static Result error(String message) {
        Result result = new Result(false, ResultCode.ERROR, null);
        result.setMessage(message); // 覆盖默认信息
        return result;
    }
}